<?php
include("Connection.php");
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of RegisterController
 *
 * @author Ryan Fahsel
 */
class AttendanceController {
    
    function addUserToSession($gtid, $sessionId) {
        mysql_query("INSERT INTO Attendance(gtid, date, session) VALUES($gtid, now(), $sessionId) ");
        
    }
    
    function checkPrevEntry($gtid, $sessionId) {
        $query=mysql_query("SELECT COUNT(*) FROM Attendance WHERE session=$sessionId AND gtid=$gtid");
        $query=mysql_fetch_array($query); 
        if($query[0]==0)
        {
            return 1;
        }
        else
        {
            return 0;
        }
        
    }
    
    function checkShirt($gtid) {
        $query=mysql_query("SELECT COUNT(*) FROM shirts WHERE gtid=$gtid");
        $query=mysql_fetch_array($query); 
        if($query[0]==0)
        {
            return 0;
        }
        else
        {
            return 1;
        }
        
    }
    
    //0=Good to go 1=Shirt 2=Previous Entry
    function checkIn($session, $gtid)
    {
        $temp=new AttendanceController();
        if($temp->checkPrevEntry($gtid, $session)==1)
        {
            if($temp->checkShirt($gtid)==1)
            {
                $temp->addUserToSession($gtid, $session);
                return "<h1 style='color:yellow'>Admit with a shirt!</h1>";
            }
            else
            {
                $temp->addUserToSession($gtid, $session);
                return "<h1 style='color:green'>Welcome to ONS!</h1>";
            }
        }
        else
        {
            return "<h1 style='color:red'>Previous Entry. DO NOT ADMIT!</h1>";
        }
    }
      
    }



//$object=new RegisterController();
//$object->RegisterT(3, "Amit", 902563529, "araahman3");
//$object->RegisterTeam(8, "hoyas", "charles", 9024, "rclay");
//$object->ProcesTeam(2);
//echo mysql_error();


//INDIVIDUAL TESTs
//$obj=new AttendanceController();
//$obj->addUserToSession(902563529,2);
//$i=$obj->checkPrevEntry(902563529, 2);
//echo "Expected: 0, Returned: ".$i."</br>";
//$i=$obj->checkPrevEntry(02563529, 2);
//echo "Expected: 1, Returned: ".$i."</br>";
//$i=$obj->checkShirt(902563529);
//echo "Expected: 0, Returned: ".$i."</br>";
//$i=$obj->checkShirt(1);
//echo "Expected: 1, Returned: ".$i."</br>";

//Check in scenario
//$i=$obj->checkIn(1,902563529);
//echo "Expected: 0, Returned: ".$i."</br>";
//$i=$obj->checkIn(1,902563529);
//echo "Expected: 2, Returned: ".$i."</br>";
//$i=$obj->checkIn(1,1);
//echo "Expected: 1, Returned: ".$i."</br>";
//$i=$obj->checkIn(1,1);
//echo "Expected: 2, Returned: ".$i."</br>";
//$i=$obj->checkIn(1, 1);
//echo $i;



echo mysql_error();

?>
